import sklearn as sk
import sklearn
from sklearn import datasets

import matplotlib.pyplot as plt
import numpy as np
import os
datapath = os.path.join("datasets", "lifesat", "")


# datas = datasets.load_boston()
# len(datas.feature_names)
# for i in range(2):
#     plt.subplot(7, 1, i+1)
#     plt.scatter(datas.data[:,i], datas.target)
#     plt.title(datas.feature_names[i])
#     plt.show()
#
# print(datas.DESCR)

# 数据加载
import pandas as pd
def load_datas():
    def prepare_country_stats(oecd_bli, gdp_per_capita):
        oecd_bli = oecd_bli[oecd_bli["INEQUALITY"] == "TOT"]
        oecd_bli = oecd_bli.pivot(index="Country", columns="Indicator", values="Value")
        gdp_per_capita.rename(columns={"2015": "GDP per capita"}, inplace=True)
        gdp_per_capita.set_index("Country", inplace=True)
        full_country_stats = pd.merge(left=oecd_bli, right=gdp_per_capita,
                                      left_index=True, right_index=True)
        full_country_stats.sort_values(by="GDP per capita", inplace=True)
        remove_indices = [0, 1, 6, 8, 33, 34, 35]
        keep_indices = list(set(range(36)) - set(remove_indices))
        return full_country_stats[["GDP per capita", 'Life satisfaction']].iloc[keep_indices]
    oecd_bli = pd.read_csv(datapath + "oecd_bli_2015.csv", thousands=',')
    gdp_per_capita = pd.read_csv(datapath + "gdp_per_capita.csv", thousands=',', delimiter='\t',
                                 encoding='latin1', na_values="n/a") #latin1 = ISO-8859-1, delimiter为备选分隔符
    country_stats = prepare_country_stats(oecd_bli, gdp_per_capita)
    X = np.c_[country_stats["GDP per capita"]]
    y = np.c_[country_stats["Life satisfaction"]]
    country_stats.plot(kind='scatter',  x ='GDP per capita', y ='Life satisfaction')
    plt.show()
    # Select a linear model
    model = sklearn.linear_model.LinearRegression()

    # Train the model
    model.fit(X, y)

    # Make a prediction for Cyprus
    X_new = [[22587]]  # Cyprus' GDP per capita
    print(model.predict(X_new))  # outputs [[ 5.96242338]]

# 数据处理
#特征处理
def feature_handle():
    # 字典转矩阵: 基本是one hot/ one key 编码
    from sklearn.feature_extraction import DictVectorizer
    data = [
        {'a':1, 'b':2,'c':3, 'e':'asdsa'},
        {'c':3, 'e':'123', 'f':'三大'},
        {'b':1, 'f':'阿萨德'},
    ]
    vec = DictVectorizer()
    data_array = vec.fit_transform(data).toarray()
    print(data_array)
    print(vec.get_feature_names())
    pass


if __name__ == '__main__':
    load_datas()
    feature_handle()

    pass

'''
panda数组索引即可是数字亦可是字符
.pivot(index="学号", columns="科目", values="成绩") 从数据中重建一张表 原表数据1-n(一个学生有3课成绩,那就有3行,)
(现在合成一行, 用科目名当列名,每列值为成绩) https://blog.csdn.net/qq_29118049/article/details/78804768
更改列名:.rename(columns={"2015": "GDP per capita"}, inplace=True)
set_index(['a', 'b'])设置索引列 可设置联合索引
.loc['行索引','列标签'] 取值
iloc[0,0] 按数字索引取值
画图：
散点图：.plot(kind='scatter',  x =' GDP  per  capita ', y ='Life  satisfaction') plt.show()
'''

'''
numpy c_按行级联
list1 = [1, 2, 3]
list2 = [4, 5, 6]
x = np.r_[list1, list2] # [1 2 3 4 5 6]
y = np.c_[list1, list2] #[[1 4] ，[2 5]，[3 6]]
给定范围, 按数量1000生成等距序列:np.linspace(0, 60000, 1000)
'''

'''
标记: plt.annotate('标记说明', xy=(标记点x, 标记点y), xytext=pos_text提示吗?还是什么之类的,
            arrowprops=dict(facecolor='black', width=0.5, shrink=0.1, headwidth=5))
设置x[0,6000],y[0,10]轴范围:plt.axis([0, 60000, 0, 10])
指定位置文本标签:plt.text(40000, 2.7, r"$\theta_0 = 0$", fontsize=14, color="r")
'''